#include "worker.h"
#include "taskQueue.h"
#include "ThreadPool.h"
#include "mysqlFunc.h"

int workerInit(tidArr_t *ptidArr, int workerNum){
    ptidArr->arr = (pthread_t *)calloc(workerNum, sizeof(pthread_t));
    ptidArr->workerNum = workerNum;
    return 0;
}
void unlock(void * arg){
    threadPool_t *threadPool = (threadPool_t *)arg;
    printf("unlock\n");
    pthread_mutex_unlock(&threadPool->mutex);
}
void *threadFunc(void * arg){
    threadPool_t *threadPool = (threadPool_t *)arg;
    train_t train;
    int netFd, taskCode, insertResult;

    //user
    int queryResult = 0;//查询结果, 查询到了=1, 没查询到=2
    char userName[20] = {0};
    char userPasswd[20] = {0};
    char userPwd[100] = {0};
    char salt[20] = {0}; 
    char *cryptPasswd;

    //file
    char fileName[100] = {0};//文件名, 来自客户端
    //char userName[20];//用户名
    int pre_id;//该文件上一层目录id
    char path[100] = {0};;//路径
    char type[2] = {0};;//d-目录文件, f-普通文件
    char *fileMd5;//目录文件md5码=NULL, 文件有md5码
    int tomb;//文件存在=1, 文件不存在=0

    //mysql
    MYSQL *conn = NULL;
    userTable_t userTable;
    fileTable_t fileTable;

    while(1){
        pthread_mutex_lock(&threadPool->mutex);       
        while(threadPool->taskQueue.queueSize <= 0 && threadPool->exitFlag == 0){
            pthread_cond_wait(&threadPool->cond, &threadPool->mutex);
        }
        if(threadPool->exitFlag == 1){
            //子线程退出
            printf("I an child thread, I am going to exit!\n");
            pthread_mutex_unlock(&threadPool->mutex);
            pthread_exit(NULL);
        }
        netFd = threadPool->taskQueue.pFront->netFd;
        taskCode = threadPool->taskQueue.pFront->taskCode;
        conn = threadPool->taskQueue.pFront->conn;
        //客户端账户和密码
        memcpy(userName, threadPool->taskQueue.pFront->userName, sizeof(threadPool->taskQueue.pFront->userName));
        memcpy(userPasswd, threadPool->taskQueue.pFront->userPasswd, sizeof(threadPool->taskQueue.pFront->userPasswd));
        deQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->mutex);
        printf("I am child thread, I got a netFd =  %d, taskCode = %d\n", netFd, taskCode);

        //业务代码
        bzero(&queryResult, sizeof(queryResult));
        switch (taskCode)
        {
        case REGISTER_APPLY:
            printf("客户端注册申请\n");
            mysqlQuery(conn, userName, &queryResult);
            printf("queryResult = %d\n", queryResult);
            if(1 == queryResult){
                //已有该用户, 返回注册失败
                train.code = REGISTER_FAID;
                printf("train.code = %d\n", train.code);
                send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
            }
            else if(-1 == queryResult){
                printf("用户表和文件表的插入\n");
                //未查询到,就新建用户
                //1、随机生成盐值
                generateSalt(salt);
                //2、生成密文 = crypt(passwd, salt);
                cryptPasswd = crypt(userPasswd, salt);
                while(cryptPasswd == NULL){
                    generateSalt(salt);
                    cryptPasswd = crypt(userPasswd, salt);
                }
                printf("cryptPasswd = %s\n", cryptPasswd);

                //3、创建家目录,在根目录下 ~/Netdisk/userName/,赋值给pwd
                bzero(&userTable, sizeof(userTable_t));
                memcpy(userTable.userName, userName, strlen(userName));
                memcpy(userTable.userSalt, salt, strlen(salt));
                memcpy(userTable.userCryptpasswd, cryptPasswd, strlen(cryptPasswd));
                memcpy(userTable.userPwd, "/", 1);
                
                //4、向mysql的user表中插入数据
                char userTableName[20] = "user";
                mysqlInsert(conn, userTableName, &userTable, &insertResult);
                if(-1 == insertResult){
                    printf("注册失败, 已有该账号");
                    train.code = REGISTER_FAID;
                    printf("train.code = %d\n", train.code);
                    send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
                    break;
                }
                else if(1 == insertResult){
                    //6、发送注册成功信号
                    bzero(&train,sizeof(train_t));
                    train.code = REGISTER_OK;
                    printf("train.code = %d\n", train.code);
                    send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
                    printf("注册成功!\n");
                }
                else{
                    printf("注册失败, 已有该账号");
                    train.code = REGISTER_FAID;
                    printf("train.code = %d\n", train.code);
                    send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
                    break;
                }
            }
            else{
                //查询错误
                printf("mysql查询错误!\n");
                continue;
            }
            break;
        case LOGIN_APPLY:
            printf("客户端登录申请\n");
            passwdVerify(conn, userName, userPasswd, &queryResult);
            printf("queryResult = %d\n", queryResult);
            if(1 == queryResult){
                //查询到了该用户,可以登录
                printf("登录成功!\n");
                bzero(&train,sizeof(train_t));
                train.code = LOGIN_OK;
                printf("train.code = %d\n", train.code);
                //发送登录成功代码和当前路径, 登录后默认路径为 /
                memcpy(fileTable.path, "/", 2);
                train.length = strlen(fileTable.path);
                memcpy(train.data, fileTable.path, strlen(fileTable.path));
                send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
            }
            else if(-1 == queryResult){
                //没有查到该用户,返回登录错误
                printf("该用户不存在, 请重新输入!\n");
                bzero(&train,sizeof(train_t));
                train.code = LOGIN_FAID;
                printf("train.code = %d\n", train.code);
                send(netFd, &train, sizeof(train_t), MSG_NOSIGNAL);
            }
            else{
                printf("mysql查询错误");
                continue;
            }
            break;
        default:
            break;
        }
        //close(netFd);
    }
}